package com.mf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mf.entity.bo.PositionBo;
import com.mf.entity.dto.dept.PositionDto;
import com.mf.entity.vo.dept.PositionVo;
import com.mf.mapper.PositionMapper;

import com.mf.service.IPositionService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.service.IRosterService;
import io.github.heollhai.common.config.exceptions.GraceException;
import io.github.heollhai.common.enums.EnableOrDisableEnum;
import io.github.heollhai.common.utils.MybatisPlusUtils;
import io.github.heollhai.common.utils.VerifyUtils;
import io.github.heollhai.common.utils.tree.TreeUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* 岗位表 服务实现类
*
* @author mf
* @since 2024-09-20
*/
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, PositionBo> implements IPositionService {
    @Resource
    private PositionMapper positionMapper;

    @Resource
    private IRosterService rosterService;
    /**
     * 查询所有启用岗位
     */
    @Override
    public List<PositionVo> listFindByEnable() {
        LambdaQueryWrapper<PositionBo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PositionBo::getStatus, EnableOrDisableEnum.ENABLE.value);
        return MybatisPlusUtils.changeDataClass(positionMapper.selectList(wrapper), PositionVo.class);
    }

    /**
     * 根据部门id 查询岗位
     */
    @Override
    public List<PositionVo> listFindTreeByDeptId(Integer deptId) {
        LambdaQueryWrapper<PositionBo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PositionBo::getDeptId, deptId)
                .eq(PositionBo::getStatus, EnableOrDisableEnum.ENABLE.value);
        return MybatisPlusUtils.changeDataClass(positionMapper.selectList(wrapper), PositionVo.class);
    }


    /**
     * 获取所有岗位按照部门id分组
     */
    @Override
    public Map<Integer, List<PositionVo>> listFindTreeByDeptIdGroup() {
        List<PositionBo> positionBos = positionMapper.selectList(null);
        List<PositionVo> positionVos = TreeUtils.getRoot(MybatisPlusUtils.changeDataClass(positionBos, PositionVo.class));
        return positionVos.stream().collect(Collectors.groupingBy(PositionVo::getDeptId));
    }

    /**
     * 根据部门id查询是否有岗位
     */
    @Override
    public Boolean isJobIdByDeptId(Integer deptId) {
        LambdaQueryWrapper<PositionBo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PositionBo::getDeptId, deptId);
        PositionBo positionBo = positionMapper.selectOne(wrapper);
        return  VerifyUtils.isNotEmpty(positionBo);
    }

    /**
     * 新增
     */
    @Override
    public Integer insert(PositionDto position) {
        PositionBo positionBo = new PositionBo();
        BeanUtils.copyProperties(position, positionBo);
        if(VerifyUtils.isEmpty(position.getId())){
            return positionMapper.insert(positionBo);
        }

        if(position.getId().equals(position.getPid())){
            GraceException.display("上级部门不能为自身");
        }

        // 如果岗位有上级那么关联部门同步为上级部门
        if(position.getPid() != 0){
            LambdaQueryWrapper<PositionBo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PositionBo::getId, position.getPid());
            PositionBo positionBo1 = positionMapper.selectOne(wrapper);
            if(!positionBo.getDeptId().equals(positionBo1.getDeptId())){
                GraceException.display("跨部门调整请确定关联部门和所属上级是否合理");
            }
        }
        return positionMapper.updateById(positionBo);
    }
    /**
     * 删除
     */
    @Override
    public Integer deleteById(Integer id) {
        if(rosterService.isJobIdByUser(id)){
            GraceException.display("该岗位下有员工，无法删除");
        }
        return positionMapper.deleteById(id);
    }

    /**
     * 获取所有启用岗位
     */
    @Override
    public List<PositionVo> listFindAllByEnable() {
        LambdaQueryWrapper<PositionBo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PositionBo::getStatus, EnableOrDisableEnum.ENABLE.value);
        return MybatisPlusUtils.changeDataClass(positionMapper.selectList(wrapper), PositionVo.class);
    }
    /**
     * 把所有启用岗位按照id分组
     */
    @Override
    public Map<Integer, List<PositionVo>> listFindAllByIdGroup() {
        List<PositionVo> positionVos = listFindAllByEnable();
        return positionVos.stream().collect(Collectors.groupingBy(PositionVo::getId));
    }
}
